#define PI 3.1415

vec2 fixuv(in vec2 uv) {

    vec2 p = (2. * uv - iResolution.xy) / min(iResolution.x, iResolution.y);
    p.x += 1.;
    p.y *= -1.0;
    p.y += 1.0;
    p *= 8.;
    return p;
}

vec3 drawGrid(in vec2 p) {
    float f = floor(p.x) + floor(p.y);
    vec3 col = mix(vec3(0.75), vec3(0.3), smoothstep(0., 1., mod(f, 2.)));
    col = mix(col, vec3(1., 0., 0.), smoothstep(abs(p.x), abs(p.x), fwidth(p.x) * 2.));
    col = mix(col, vec3(0., 0., 1.), smoothstep(abs(p.y), abs(p.y), fwidth(p.y) * 2.));
    return col;
}

vec3 drawSegment(in vec2 p, in vec2 sp, in vec2 ep, in float w, in vec3 color, in vec3 col) {

    vec2 v1 = ep - sp;
    vec2 v2 = sp - ep;
    vec2 a1 = p - sp;
    vec2 a2 = p - ep;

    if(dot(v1, a1) > 0. && dot(v2, a2) > 0.001) {
        float theta = dot(v1, a1) / pow(length(v1), 2.);
        //计算底边长度
        float bl = length(v1 * theta);
        //邻边长度
        float al = sqrt(pow(length(a1), 2.) - pow(bl, 2.));
        col = mix(col, color, smoothstep(w / 2., (w / 2.) * 0.99, al));
    }

    return col;
}

//p ：当前像素点的位置
//w :线段的宽度
// amplitude 振幅（即是线段距离水平点的长度）
// cycle 周期（即是2Π的长度）
// frequency 频率 （即是在2Π之间的跳动次数）
float sinLine(in vec2 p, in vec2 sp, in float w, in float amplitude, in float cycle, in float frequency) {
    //y = 振幅* sin(周期*p.x+相对位置（相移）)+垂直位移
    vec2 v = sp - vec2(-0.000001, 0.);
    vec2 d = p - sp;
    if(dot(v, d) > 0.) {
        // 周期
        float t = 2. * PI / cycle;
        // y位置
        float y = amplitude * sin(t * (p.x - sp.x));
        vec2 sxy = vec2(p.x, y);
        float d = length(sxy - p);
        return smoothstep(w, w * 0.99, d);

    }
    return 0.;
}

//p ：当前像素点的位置
//w :线段的宽度
// amplitude 振幅（即是线段距离水平点的长度）
// cycle 周期（即是2Π的长度）
// frequency 频率 （即是在2Π之间的跳动次数）
vec3 sinLine2(in vec2 p, in vec2 sp, in vec2 ep, in float w, in vec3 color, in vec3 col, in float amplitude, in float cycle, in float frequency ,in float rp) {

    vec2 v1 = ep - sp;
    vec2 v2 = sp - ep;
    vec2 a1 = p - sp;
    vec2 a2 = p - ep;

    if(dot(v1, a1) > 0. && dot(v2, a2) > 0.001) {
        float theta = dot(v1, a1) / pow(length(v1), 2.);
        //计算底边长度
        float bl = length(v1 * theta);
        float t = 2. / PI * cycle*frequency;
        float y = amplitude * sin(t * bl + rp * 10.);
        vec2 norml = normalize(v1);
        float x = bl;
        vec2 lp;
        lp.x = x * norml.x - y * norml.y;
        lp.y = y * norml.x + x * norml.y;
        lp += sp;
        float b = length(lp - p);
        w = w / 2.;
        col = mix(col, color, smoothstep(w, w * 0.7, b));
    }

    return col;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = fixuv(fragCoord);
    float w = fwidth(p.x);
    vec3 col;
    col = drawGrid(p);
    col = mix(col, vec3(1., 1., 0.), sinLine(p, vec2(1, 0.), w * 10., (sin(iTime)), 1., 2.));
    vec2 sp = vec2(10, 10.);

    float radin = (PI / 180. * 0.) ;
    float len = 5.;
    vec2 ep = sp + vec2(cos(radin), sin(radin)) * len;
    vec3 color = vec3(1., 0.3, 0.45);
    col = drawSegment(p, sp, ep, w * 1., vec3(0., 1., 1.), col);
    col = sinLine2(p, sp, ep, w * 50., color, col,  1.,  2.,1.+abs(sin(iTime))* 10., 0.);
    fragColor = vec4(col, 1.);
}